library(plyr)
library(estimatr)
library(emmeans)
library(tidyverse)
library(magrittr)
library(broom)
library(gt)
library(modelsummary) 

d1 <- "https://github.com/thomasjwood/full_fact/raw/main/ff_mt.rds" %>% 
  url %>% 
  gzcon %>% 
  readRDS %>% 
  mutate(
    out_agree = out_agree %>% 
      mapvalues(
        c("Tend to disagree",
          "Tend to agree"),
        c("Disagree",
          "Agree")
      ) %>% 
      factor(
        c("Strongly disagree",
          "Disagree",
          "Neither agree nor disagree",
          "Agree",
          "Strongly agree")
      ),
    out_true = out_true %>% 
      factor(
        c("True",
          "Probably true",
          "Not sure",
          "Probably false",
          "False") %>% 
          rev
      ),
    agree_num = out_agree %>% 
      as.numeric,
    true_num = out_true %>% 
      as.numeric,
    cond = cond %>% 
      factor(
        c("cond_items",
          "cond_misinfo",
          "cond_corr")
      ),
    employ2 = employ %>% 
      mapvalues(
        c("Retired",
          "Full-time parent, homemaker",
          "Self employed", 
          "Student/Pupil",
          "Employed full-time",
          "Employed part-time", 
          "Unemployed and not looking for a job/Long-term sick or disabled", 
          "Unemployed but looking for a job"),
        c("Retired",
          "Unemployed",
          "Employed", 
          "Student",
          "Employed",
          "Employed", 
          "Unemployed", 
          "Unemployed")
      ) %>% 
      factor(
        c("Retired",
          "Employed",
          "Unemployed",
          "Student")
      ),
    ideo2 = ideo %>% 
      mapvalues(
        c("Left1", "1", "2", "3", "4",
          "5", "6",
          "7", "8", "9",  "10", "Right10", 
          
          "DK", "Don't know", "Prefer not to say"),
        c("Left", "Moderate", "Right", "DK") %>% 
          rep(
            c(5, 2, 5, 3)
          )
      ) %>% 
      factor(
        c("Left", "Moderate", "Right", "DK")
      ),
    ideonum = ideo %>% 
      mapvalues(
        c("Left1", "1", "2", "3", "4",
          "5", "6",
          "7", "8", "9",  "10", "Right10", 
          
          "DK", "Don't know", "Prefer not to say"),
        c(1, 1:10, 10, NA, NA, NA)
      ) %>% 
      as.character %>% 
      as.numeric,
    age = 2020 %>% 
      subtract(
        birthyr
      ) %>% 
      cut(
        breaks = c(-Inf, 26, 40, 60, Inf),
        labels = c(
          "18-26", "27-40", "41-60", "61+"
        )
      )
    )

cor(d1$agree_num, d1$true_num)

d1$com_num <- d1$agree_num %>% 
  add(
    d1$true_num
  ) %>% 
  divide_by(2)

# count of correction exposures

d1 %<>% 
  left_join(
    d1 %>% 
      filter(wave == "wave 1") %>% 
      select(
        caseid, country, cond
      ) %>% 
      group_by(
        caseid, country
      ) %>% 
      summarize(
        cnt_corr = cond %>% str_detect("_corr") %>% sum,
        cnt_mis  = cond %>% str_detect("_misinfo") %>% sum
      )
    )


# attrition check--gen dummy for 1 wave respondents

d1 %<>% 
  left_join(
    d1 %>% 
      select(
        country, caseid, wave
      ) %>% 
      group_by(
        country, caseid, wave
      ) %>% 
      slice(1) %>% 
      group_by(
        country, caseid
      ) %>% 
      summarize(
        waves_done = wave %>% unique %>% length
      ) %>% 
      mutate(
        attrited = waves_done %>% 
          equals(1) %>% 
          ifelse(0, 1)
      )
  ) %>% 
  select(-waves_done) %>% 
  filter(
    country %>% 
      equals("nigeria") %>% 
      not
    )

t1 <- d1 %>% 
  mutate(
    country = issue %>%
      str_detect(
        "global|saltwater"
      ) %>%
      ifelse(
        "Multi-country",
        country
      )
  ) %>%
  filter(
    wave == "wave 1" &
    country %>% 
      equals("Multi-country") %>% 
      not
    ) %>%
  group_by(country, caseid) %>% 
  slice(1) %>% 
  group_by(
    country
  ) %>% 
  nest 

t1$mis_mods <- t1$data %>% 
  map(
    \(i)
    glm(
      attrited ~  age + employ2 + ideo2 + gender + cnt_corr + cnt_mis,
      data = i,
      family = binomial()
      )
    )


# missingnesmodelsummary
 
 
t1$mis_mods %>%
  set_attr(
    "names",
    t1$country
  ) %>% 
  modelsummary(
    stars = T,
    output = "gt",
    coef_rename = c(
      c("(Intercept)" = "Intercept",  
        "age27-40" = "Age 27-40",
        "age41-60" = "41-60",
        "employ2Employed" =   "Employment Employed", 
        "employ2Unemployed" = "        Unemployed",
        "employ2Student" = "Student",
        "ideo2Moderate" = "Ideology Moderate",
        "ideo2Right" = "Right", 
        "ideo2DK" = "Don't Know",
        "genderMale" = "Male",
        "cnt_corr" = "Experimental condition Count of corrections",
        "cnt_mis" = "Experimental condition Count of misinformation")
      ),
    coef_omit = "age61\\+|genderIn another way|genderPrefer not to answer"
    ) 

